﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orchard.Service;
using Orchard.Entity;

namespace EF_Orchard的底层.Controllers
{
    public class HomeController : Controller
    {
        IWeb_ExceptionLogManager _web_ExceptionLogManager;
        IWebLogsManager _iWebLogsManager;
        public HomeController(IWeb_ExceptionLogManager web_ExceptionLogManager, IWebLogsManager iWebLogsManager)
        {
            _web_ExceptionLogManager = web_ExceptionLogManager;
            _iWebLogsManager = iWebLogsManager;
        }

        public ActionResult Index(int sort = 1)
        {
            ViewBag.Message = "欢迎使用 ASP.NET MVC!";

            var model = _iWebLogsManager.Fetch(i => i.Info != null, i => i.Asc(j => j.OccurTime));
            if (sort != 1)
                model = _iWebLogsManager.Fetch(i => i.Info != null, i => i.Desc(j => j.OccurTime));

            ViewBag.Log = _web_ExceptionLogManager.GetWeb_ExceptionLog();
            return View(model);
        }
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            int startrec, num;
            int.TryParse(form["startrec"], out startrec);
            int.TryParse(form["num"], out num);
            var model = _iWebLogsManager.Fetch(i => i.Info != null, i => i.Asc(j => j.OccurTime), startrec, num);

            return View(model);
        }

        public ActionResult Edit(string id)
        {
            Web_Logs model = _iWebLogsManager.Get(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(Web_Logs entity)
        {
            if (ModelState.IsValid)
            {
                 _iWebLogsManager.Update(entity);
            }
            return RedirectToAction("Index");
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
